{% extends 'master.html' %}


{% block master %}
{% csrf_token %}
<div class="x-nav">
	<span class="layui-breadcrumb">
        <a href="">任务管理</a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
      </a>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="id_taskname" name="taskname" placeholder="任务名称" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" id="id_assetkey" name="assetkey" placeholder="资产标识" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select name="contrller" id ='id_tasktype'>
                                <option value="">任务类型</option>
                                <option value="安全扫描">安全扫描</option>
                                <option value="结果同步">结果同步</option>
                            </select>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select class="container" id="id_is_use">
                                <option value="">任务状态</option>
                                <option value="1">待执行</option>
                                <option value="2">执行中</option>
                                <option value="3">已暂停</option>
                                <option value="4">已完成</option>
                                <option value="5">已结束</option>
                            </select>
                        </div>
                    </form>
                    <div class="layui-input-inline layui-show-xs-block UserTable">
                        <button class="layui-btn" data-type="reload" lay-submit="" lay-filter="sreach">
                            <i class="layui-icon">&#xe615;</i></button>
                    </div>
                </div>
                <xblock>
                    <div class="layui-card-header UserTable">
                        <button class="layui-btn" onclick="x_admin_show('扫描结果同步', '{% url "tasksync" %}')"><i class="layui-icon layui-icon-download-circle"></i>扫描同步</button>
                        <button class="layui-btn" onclick="x_admin_show('任务新建', '{% url "scantask" %}')"><i class="layui-icon layui-icon-add-1"></i>新建任务</button>
                    </div>
                </xblock>
                <!-- task table-->
                <div class="layui-card-body">
                    <table class="layui-table layui-form" id="task_table" lay-filter="task"></table>
                </div>
                <!--end task table-->
            </div>
        </div>
    </div>
</div>

<script>
var csrf = $('input[name="csrfmiddlewaretoken"]').val();

layui.use('table', function(){
    var table = layui.table;
    //方法级渲染
    table.render({
        elem: '#task_table'
        ,url: "{% url 'tasklist' %}"
        ,method: 'post'
        ,where: {'csrfmiddlewaretoken':csrf}
        ,cols: [[
            {checkbox: true, fixed: true}
            ,{field:'task_name', title: '任务名称', sort: true, fixed: true, event:'Task'}
            ,{field:'task_type', title: '任务类型'}
            ,{field:'task_target', title: '扫描目标', sort: true}
            ,{field:'task_status', title: '任务状态', sort: true}
            ,{field:'task_starttime', title: '创建时间',sort: true}
            ,{field:'task_scanner', title: '扫描节点'}
            ,{field:'task_user', title: '申请人员',sort: true}
            ,{ title: '高级选项',fixed: 'right', align:'center', toolbar: '#barDemo'}
        ]]
        ,id: 'testReload'
        ,page: true
        ,limit: 15
        ,text:{none: '目前暂无数据'}
    });
    // table功能
    table.on('tool(task)', function (obj) {
        var data = obj.data.task_id;
        if (obj.event === 'Task'){
            url = "{% url 'taskdetails' 'key' %}";
            url.replace(/key/, data);
            x_admin_show('任务详情', url, 600)
        }else if(obj.event === 'run'){
            layui.confirm('该操作为敏感操作<br>点击确认,执行该任务',function(index){
                url = "{% url 'taskaction' 'key' 'run' %}";
                url = url.replace(/key/,data);
                $.get(url,function(res){
        		layer.msg(res.error);
        		setTimeout(function(){
		    		location.replace(location.href);
		    	},2000)
            });
            })
        }else if(obj.event === 'stop'){
            layer.confirm('该操作为敏感操作<br>点击确认,执行该任务',function(index){
                url = "{% url 'taskaction' 'key' 'stop' %}";
                url = url.replace(/key/,data);
                $.get(url,function(res){
                    layer.msg(res.error);
                    setTimeout(function(){
                        location.replace(location.href);
                    },2000)
                });
            })
        } else if(obj.event === 'detail'){
            url = "{% url 'taskdetails' '0' %}";
            url = url.replace(/0/,data);
            x_admin_show('任务详情',url,600);
        }
    });


})
</script>
<!--操作栏选项，后期会统一 -->
<script type="text/html" id="barDemo">
<div class="layui-btn-group">
	{% verbatim %}
		{{# if(d.task_status=='待执行'){ }}
			<a class="layui-btn layui-btn-xs" lay-event="run"><i class="layui-icon">&#xe652;</i>执行</a>
		{{# } else if(d.task_status=='执行中'){ }}
			<a class="layui-btn layui-btn-xs" lay-event="stop"><i class="layui-icon">&#x1007;</i>取消</a>
		{{# } }}
	{% endverbatim %}
	<a class="layui-btn layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe60a;</i>详情</a>
</div>
</script>

{% endblock %}